package cn.wolfcode.springboot_producer._01_hello;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @RequestMapping("/send")
    public String send(String msg,String tag){
       /* MessageBuilder<String> builder = MessageBuilder.withPayload(msg);
        Message<String> message = builder.build();*/
        rocketMQTemplate.syncSend("01-boot-hello:"+tag,msg);
        return "发送成功";
    }
    @RequestMapping("/filterSend")
    public String filterSend(String msg,String tag,String age){
        Map<String,Object> map = new HashMap<>();
        map.put("age",age);
        map.put("weight","180");
        map.put(MessageConst.PROPERTY_KEYS,"123");
        rocketMQTemplate.convertAndSend("01-boot-hello:"+tag,msg,map);
        return "发送成功";
    }
    @RequestMapping("/asyncSend")
    public String asyncSend(String msg){
        rocketMQTemplate.asyncSend("01-boot-hello", msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });
        return "发送成功";
    }
    @RequestMapping("/sendOneWay")
    public String sendOneWay(String msg){
        rocketMQTemplate.sendOneWay("01-boot-hello",msg);
        return "发送成功";
    }
    @RequestMapping("/delaySend")
    public String delaySend(String msg){
        MessageBuilder<String> builder = MessageBuilder.withPayload(msg);
        Message<String> message = builder.build();
        rocketMQTemplate.syncSend("boot-send-delay",message
                ,5000,3);
        /*DefaultMQProducer producer = rocketMQTemplate.getProducer();
        org.apache.rocketmq.common.message.Message message1 = new org.apache.rocketmq.common.message.Message("topic",
                "123".getBytes());
        message1.setDelayTimeLevel(3);
        producer.send(message1);*/
        return "发送成功";
    }
}
